﻿using System;
using System.Collections.Generic;
using ESoft.Core.Data;
using ESoft.Core.Initialize;
using ESoft.Core.Logging;
using Microsoft.EntityFrameworkCore;

namespace ESoft.Core.Model
{
    public class DataContext : DbContext
    {
        private IDbContextOption _dbContextOption;

        private readonly Logging.ILogger logger = LogManager.GetLogger(typeof(DataContext));

        public DataContext(IDbContextOption dbContextOption)
        {
            _dbContextOption = dbContextOption;
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseMySQL(_dbContextOption.Connetion);
        }


        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            ////移除一对多的级联删除
            //modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
          
            ////注册实体配置信息
            ICollection<IEntityMappingConfiguration> entityMappers = DatabaseInitializer.EntityMappers;
            foreach (IEntityMappingConfiguration mapper in entityMappers)
            {
                mapper.Map(modelBuilder);
            }
        }
    }
}
